/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication4.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javaapplication4.controller.AbstractController;
import javaapplication4.model.Contact;
import javaapplication4.model.observer.Observer;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 *
 * @author mathieubakrim
 */
public class ListAlphaView extends JFrame implements Observer{
    private AbstractController controller;    
    private JPanel container = new JPanel();
    
    private ArrayList<Contact> contactDatas;
    
    private String [] headers ={"Last name","First name", "Phone number","Professional address",
        "Personal address", "Home page", "Professional email", "Personnal email"};
    
    private Object[][] datas;

    
    
    
    
    public ListAlphaView(AbstractController controller) {
        this.setSize(450, 470);
        this.setTitle("Create a contact");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(true);
        this.contactDatas = controller.getContactsList();
        initComposant();
        this.controller = controller;
        this.setContentPane(container);
        this.setVisible(true);
    }
    
    private void initComposant() {
        
        JPanel basic = new JPanel();
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
        add(basic);
       
        JPanel topPanel = new JPanel(new BorderLayout(0, 0));
        //topPanel.setMaximumSize(new Dimension(450, 0));
        JLabel title = new JLabel("Alpha Contact list");
        title.setBorder(BorderFactory.createEmptyBorder(0, 25, 0, 0));
        topPanel.add(title);
        
        JSeparator separator = new JSeparator();
        separator.setForeground(Color.gray);

        topPanel.add(separator, BorderLayout.SOUTH);

        basic.add(topPanel);
        
        
             
        
        
        System.out.println("ContactDatas données: ");
        System.out.println(contactDatas.get(0));

        //datas = new Object[contactDatas.size()][headers.length];
        String[][] infosContact = new String[contactDatas.size()][headers.length];
        
        for(int i=0;i<contactDatas.size();i++){
            for(int j =0; j<headers.length;j++){
                
                infosContact[i][j] = contactDatas.get(i).getLastName();
                infosContact[i][j] = contactDatas.get(i).getFirstName();
                infosContact[i][j] = contactDatas.get(i).getPhoneNumber();
                infosContact[i][j] = contactDatas.get(i).getAddressPro();
                infosContact[i][j] = contactDatas.get(i).getAddressPer();
                infosContact[i][j]= contactDatas.get(i).getHomePage();
                infosContact[i][j] = contactDatas.get(i).getEmailPro();
                infosContact[i][j] = contactDatas.get(i).getEmailPer();
            }
        }
        
        JTable alphaList = new JTable(infosContact, headers);  
        
        getContentPane().add(new JScrollPane(alphaList.getTableHeader()), BorderLayout.NORTH);
        getContentPane().add(new JScrollPane(alphaList), BorderLayout.CENTER);
        
        setVisible(true);
        basic.add(alphaList);
        
        JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        JButton cancel = new JButton("Go back");
        System.out.println("1");
        cancel.addActionListener(new ListAlphaView.CancelListener());
        
        
        
        
        bottom.add(cancel);
        
        
        basic.add(bottom);
        
        container.add(basic);
        
        
        
    } 
    class CancelListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
                setVisible(false); //you can't see me!
                dispose(); //Close the form window
            
        }
    }
    
    
    
    @Override
    public void update(String str) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    
}
